# _*_ coding:utf-8 _*_
# @Time :2021/3/30 9:56
# @Author :yanxia


from application.libs import const
from application.apps.collect.service.emial_collect import check_replytest_recipient, check_test_emaildata, \
    send_email_html, check_replytest_cc

from application import db
from application.models.models import ReplyTest, TestReportTable


def check_emaildata(id):
    result = db.session.query(ReplyTest.project, ReplyTest.pversion, ReplyTest.platform,
                              ReplyTest.mantis_adress, ReplyTest.remarks).filter(
        ReplyTest.id == id).all()
    result_dic = {
        "email_theme": result[0][0] + "-v" + result[0][1],
        "project": result[0][0],
        "pversion": result[0][1],
        "platform": result[0][2],
        "mantis_adress": result[0][3],
        "remarks": result[0][4]
    }
    return result_dic


def check_email_recipient(pid):
    result = db.session.query(TestReportTable.addressee, TestReportTable.cc).filter(TestReportTable.id == pid).all()
    return result


def send_reply_test(data):
    pid = int(data["pid"])
    replyTest = db.session.query(ReplyTest).filter(ReplyTest.id == pid).first()
    if not replyTest:
        return const.OPERATE_FAIL
    receiver_list = check_replytest_recipient(pid)
    if not isinstance(receiver_list, list):
        raise TypeError
    cc_list = check_replytest_cc(pid)

    if not isinstance(cc_list, list):
        raise TypeError

    html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>邮件</title>
</head>
<body>
<div style="clear:both;">
	<div style="margin: 0px; padding: 0px; border: 0px; outline: 0px; color: rgb(0, 0, 0); font-family: Tahoma, Arial, STHeiti, SimSun; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; text-indent: 0px; text-transform: none; text-decoration-style: initial; text-decoration-color: initial; clear: both;"
	class=" __aliyun_node_has_color">
		<p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;line-height:29.0px;background:#ffffff;"
		class="MsoNormal __aliyun_node_has_bgcolor">
			<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
			class=" __aliyun_node_has_color">
				Dear All：
			</span>
		</p>
		<p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;text-indent:28.0px;line-height:29.0px;background:#ffffff;"
		class="MsoNormal __aliyun_node_has_bgcolor">
			<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:微软雅黑;color:#000000;font-size:14.0px;"
			class=" __aliyun_node_has_color">
				<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:微软雅黑;">
					邮件已收到，
				</span>
				<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:微软雅黑;">
					Mantis地址详情如下。
				</span>
			</span>
		</p>
		<p style="margin:.0px .0px .0px;text-align:justify;font-family:Calibri;font-size:14.0px;line-height:29.0px;background:#ffffff;"
		class="MsoNormal __aliyun_node_has_bgcolor">
			<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
			class=" __aliyun_node_has_color">
				&nbsp;
			</span>
		</p>
		<div class="aym_table_wrap" style="overflow: hidden; display: table; width: 464px; height: 156px; margin: 0px;">
			<table style="font-family:Times New Roman;font-size:13.3px;border-collapse:collapse;margin-left:.0px;border:none;"
			border="0" cellspacing="0" class="MsoNormalTable">
				<tbody>
					<tr>
						<td style="padding:.0px 7.0px;border-width:1.0px;border-style:solid;border-color:windowtext;background:#f2f4f7;"
						class=" __aliyun_node_has_bgcolor" colspan="2" valign="top" width="432">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: center; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:15.0px;"
								class=" __aliyun_node_has_color">
									Mantis筛选条件
								</span>
							</p>
						</td>
					</tr>
					<tr>
						<td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="102">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									项目：
								</span>
							</p>
						</td>
						<td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="331">
							<p style="margin: 0px; font-size: 14px; text-align: left; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="font-family:微软雅黑;">
									%(project)s
								</span>
							</p>
						</td>
					</tr>
					<tr>
						<td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="102">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									产品版本：
								</span>
							</p>
						</td>
						<td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="331">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: left; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									%(pversion)s
								</span>
							</p>
						</td>
					</tr>
					<tr>
						<td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="102">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									平台：
								</span>
							</p>
						</td>
						<td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="331">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: left; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									%(platform)s
								</span>
							</p>
						</td>
					</tr>
					<tr>
						<td style="padding:.0px 7.0px;border-left:1.0px solid windowtext;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#f2f4f7;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="102">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: right; line-height: 29px; height: auto;"
							class="MsoNormal">
								<span style="margin:.0px;padding:.0px;border:.0px;outline:.0px;font-weight:700;font-family:微软雅黑;color:#000000;font-size:14.0px;"
								class=" __aliyun_node_has_color">
									Mantis地址：
								</span>
							</p>
						</td>
						<td style="padding:.0px 7.0px;border-left:none;border-right:1.0px solid windowtext;border-top:none;border-bottom:1.0px solid windowtext;background:#ffffff;"
						class=" __aliyun_node_has_bgcolor" valign="top" width="331">
							<p style="margin: 0px; font-family: Calibri; font-size: 14px; text-align: left; line-height: 29px; height: auto;"
							class="MsoNormal">
								<a href="%(mantis_adress)s"
								target="_blank">
									点击我跳转
								</a>
							</p>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
	<br class="Apple-interchange-newline">
</div>
</body>
</html>
    """

    test_data = check_test_emaildata(pid)
    platform = "、".join(eval(test_data["platform"]))
    data_html = html % {'project': test_data["project"], 'pversion': test_data["pversion"], 'platform': platform,
                        'mantis_adress': test_data["mantis_adress"]}
    msg = const.OPERATE_SUCCESS
    try:
        send_email_html(receiver_list, cc_list, "回复：【提测】" + test_data["email_theme"], data_html)
    except:
        msg = const.OPERATE_FAIL
    return msg
